package com.hlx.controller;

import com.hlx.entiry.vo.MessageVO;
import com.hlx.service.ChatHistoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {

	private final ChatHistoryService chatHistoryService;

	private final ChatMemory chatMemory;

	/**
	 * 获取指定业务类型的所有会话id
	 * @param type
	 * @return
	 */
	@GetMapping("/{type}")
	public List<String> histories(@PathVariable("type") String type) {
		log.info("获取{}业务类型的id", type);
		return chatHistoryService.getChatIds(type);
	}

	@GetMapping("/{type}/{chatId}")
	public List<MessageVO> getHistory(@PathVariable("type") String type,
									  @PathVariable("chatId") String chatId) {
		log.info("获取{}业务类型{}的会话记录", type, chatId);
		List<Message> messages = chatMemory.get(chatId);
		if (messages == null){
			return List.of();
		}
		return messages.stream().map(MessageVO::new).toList();
	}
}
